package com.lyf.scm.common.enums;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * description:
 * es聚合公式枚举
 *
 * @author: haodong
 * @data: 2020/11/5 16:20
 */
public enum EsSumColumnEnum {

    /**
     * 预测计划量
     */
    PREDICT_SALES("predict_sales", 1, "doc['predict_sales'].value"),
    /**
     * 上次预测计划量
     */
    LAST_PREDICT_SALES("last_predict_sales", 1, "doc['last_predict_sales'].value"),
    /**
     * 预测差异
     */
    PREDICT_SALES_DIFFERENCE("predict_sales_difference", 1, "doc['predict_sales'].value - doc['last_predict_sales'].value"),
    /**
     * 预测差异比例
     */
    PREDICT_SALES_SCALE("predict_sales_scale", 4, "doc['predict_sales'].value == 0 || (doc['predict_sales'].value - doc['last_predict_sales'].value) == 0 ? null : (doc['predict_sales'].value - doc['last_predict_sales'].value) / doc['predict_sales'].value"),
    /**
     * 二级确认量
     */
    CONFIRM_L2("confirm_l2", 1, "doc['confirm_l2'].value"),
    /**
     * 二级预测确认差异
     */
    PREDICT_CONFIRM_L2_DIFFERENCE("predict_confirm_l2_difference", 1, "doc['confirm_l2'].value == 0 ? 0 : doc['confirm_l2'].value - doc['predict_sales'].value"),
    /**
     * 二级预测确认差异比例
     */
    PREDICT_CONFIRM_L2_SCALE("predict_confirm_l2_scale", 4, "doc['confirm_l2'].value == 0 || (doc['confirm_l2'].value - doc['predict_sales'].value) == 0 ? null : (doc['confirm_l2'].value - doc['predict_sales'].value) / doc['confirm_l2'].value"),
    /**
     * 三级确认量
     */
    CONFIRM_L3("confirm_l3", 1, "doc['confirm_l3'].value"),
    /**
     * 三级预测确认差异
     */
    PREDICT_CONFIRM_L3_DIFFERENCE("predict_confirm_l3_difference", 1, "doc['confirm_l3'].value == 0 ? 0 : doc['confirm_l3'].value - doc['predict_sales'].value"),
    /**
     * 三级预测确认差异比例
     */
    PREDICT_CONFIRM_L3_SCALE("predict_confirm_l3_scale", 4, "doc['confirm_l3'].value == 0 || (doc['confirm_l3'].value - doc['predict_sales'].value) == 0 ? null : (doc['confirm_l3'].value - doc['predict_sales'].value) / doc['confirm_l3'].value"),

    /**
     * 四级确认量
     */
    CONFIRM_L4("confirm_l4", 1, "doc['confirm_l4'].value"),
    /**
     * 四级预测确认差异
     */
    PREDICT_CONFIRM_L4_DIFFERENCE("predict_confirm_l4_difference", 1, "doc['confirm_l4'].value == 0 ? 0 : doc['confirm_l4'].value - doc['predict_sales'].value"),
    /**
     * 四级预测确认差异比例
     */
    PREDICT_CONFIRM_L4_SCALE("predict_confirm_l4_scale", 4, "doc['confirm_l4'].value == 0 || (doc['confirm_l4'].value - doc['predict_sales'].value) == 0 ? null : (doc['confirm_l4'].value - doc['predict_sales'].value) / doc['confirm_l4'].value"),

    /**
     * 二级上次确认量
     */
    LAST_CONFIRM_L2("last_confirm_l2", 1, "doc['last_confirm_l2'].value"),
    /**
     * 三级上次确认量
     */
    LAST_CONFIRM_L3("last_confirm_l3", 1, "doc['last_confirm_l3'].value"),
    /**
     * 四级上次确认量
     */
    LAST_CONFIRM_L4("last_confirm_l4", 1, "doc['last_confirm_l4'].value"),
    /**
     * 二级确认状态
     */
    CONFIRM_STATUS_L2("confirm_status_l2", 2, "doc['confirm_status_l2'].value"),
    /**
     * 三级确认状态
     */
    CONFIRM_STATUS_L3("confirm_status_l3", 2, "doc['confirm_status_l3'].value"),
    /**
     * 四级确认状态
     */
    CONFIRM_STATUS_L4("confirm_status_l4", 2, "doc['confirm_status_l4'].value"),
    /**
     * 二级确认目标
     */
    CONFIRM_TARGET_L2("confirm_target_l2", 1, "doc['confirm_target_l2'].value"),
    /**
     * 三级确认目标
     */
    CONFIRM_TARGET_L3("confirm_target_l3", 1, "doc['confirm_target_l3'].value"),
    /**
     * 四级确认目标
     */
    CONFIRM_TARGET_L4("confirm_target_l4", 1, "doc['confirm_target_l4'].value"),
    /**
     * 需求分配量
     */
    REQUIRE_DISTRIBUTION_QTY("require_distribution_qty", 1, "doc['require_distribution_qty'].value"),
    /**
     * 上次需求分配量
     */
    LAST_REQUIRE_DISTRIBUTION_QTY("last_require_distribution_qty", 1, "doc['last_require_distribution_qty'].value"),
    /**
     * 需求确认量
     */
    REQUIRE_CONFIRM_QTY("require_confirm_qty", 1, "doc['require_confirm_qty'].value"),
    /**
     * 上次需求确认量
     */
    LAST_REQUIRE_CONFIRM_QTY("last_require_confirm_qty", 1, "doc['last_require_confirm_qty'].value"),
    /**
     * 销售计划金额
     */
    SALES_PLAN_AMOUNT("salesPlanAmount", 1, "doc['plan_type'].value != '1' && doc['plan_type'].value != '6' && doc['plan_type'].value != '7' ? (doc['predict_sales'].value * doc['averagePrice'].value) : 0"),
    /**
     * 销售计划量
     */
    SALES_PLAN_QTY("salesPlanQty", 1, "doc['predict_sales'].value"),
    /**
     * 预测计划金额
     */
    FORECAST_PLAN_AMOUNT("forecastPlanAmount", 1, "doc['plan_type'].value == '17' ? (doc['predict_sales'].value * doc['averagePrice'].value) : 0"),
    /**
     * 销售预测计划量
     */
    FORECAST_PLAN_QTY("forecastPlanQty", 1, "doc['plan_type'].value == '17' ? doc['predict_sales'].value : 0"),
    /**
     * 安全库存
     */
    SS("ss", 1, "doc['ss'].value"),
    /**
     * 期末库存（当前实时库存）
     */
    SALE_INVENTORY("saleInventory", 1, "doc['saleInventory'].value"),
    /**
     * 单品平均价格
     */
    AVERAGE_PRICE("averagePrice", 1, "doc['averagePrice'].value"),
    /**
     * 区域
     */
    AREA("area", 1, "doc['area'].value");

    /**
     * 字段
     */
    private String Column;
    /**
     * 聚合类型
     */
    private Integer Type;
    /**
     * 获取公式
     */
    private String formula;

    EsSumColumnEnum(String column, Integer type, String formula) {
        Column = column;
        Type = type;
        this.formula = formula;
    }

    public String getColumn() {
        return Column;
    }

    public void setColumn(String column) {
        Column = column;
    }

    public Integer getType() {
        return Type;
    }

    public void setType(Integer type) {
        Type = type;
    }

    public String getFormula() {
        return formula;
    }

    public void setFormula(String formula) {
        this.formula = formula;
    }

    public static List<EsSumColumnEnum> getEsSumColumnList(List<String> columnList) {
        return Stream.of(EsSumColumnEnum.values())
                .filter(v -> columnList.stream().anyMatch(e -> e.equals(v.Column)))
                .collect(Collectors.toList());
    }

    public static List<String> getNotConversionTarget(){
        List<String> resultList = new ArrayList<>();
        for (EsSumColumnEnum value : EsSumColumnEnum.values()) {
            if (value.getColumn().contains("status") || value.getColumn().contains("scale")){
                resultList.add(value.getColumn());
            }
        }
        return resultList;
    }
}
